Imports DotNetPm.Business.Questionnaire.Taker
Imports System.Xml

Partial Class Secure_QuestionnaireTaker
    Inherits System.Web.UI.Page


    Private qNode As QuestionnaireNode
    'Private cNode As IQuestionNode
    Private uc1 As Control

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

        If Page.IsPostBack Then
            qNode = Session("QuestionnaireNode")
        End If
        If qNode Is Nothing Then
            Dim id As String = Page.Request.QueryString("id")
            If String.IsNullOrEmpty(id) Then
                qNode = Session("QuestionnaireNode")
            Else
                qNode = QuestionnaireTakerODS.Provider.BuildQuestionnaireNode(Page.Request.QueryString("id"))
                qNode.CurrentNode = qNode.FirstQuestionNode
                qNode.GetAnswerFromDB()
            End If
            If qNode Is Nothing Then
                Response.Redirect("Default.aspx")
            End If
            Session("QuestionnaireNode") = qNode

            'Dim ht As Hashtable = CType(Session("_Taker_AnswerTable"), Hashtable)
            'Response.Write(ht.Count & "<br/>")
            'For Each key As Guid In ht.Keys
            '    Response.Write("Key = " & key.ToString & ", value = " & ht(key) & "<br/>")
            'Next

        End If
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        LoadMyControl()
        If qNode.CurrentNode Is Nothing Then
            Exit Sub
        End If
        Button1.Enabled = True
        Button2.Enabled = True
        Button3.Enabled = qNode.IsGoPreviousable
        Button4.Enabled = qNode.IsGoNextable
        Button5.Enabled = True
        QuestionNav.DataSource = qNode.Instrument.Questions
        QuestionNav.DataBind()
        With QuestionnaireHeader1
            .Heading1 = qNode.Instrument.Name
            .Heading2 = "Center for Research in Chronic Disorders"  'qNode.Instrument.Description
            .InstrumentID = qNode.InstrumentID
            .StudyID = qNode.StudyID
            .UserName = Context.User.Identity.Name
            .VisitNumber = qNode.VisitNumber
            .AdminDate = Now
        End With

    End Sub

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        If qNode.CurrentNode Is Nothing Then
            Exit Sub
        End If
        Dim answer As String = qNode.FindAnswer(qNode.CurrentNode.UID)
        'Response.Write("Answer = " & answer & "<br/>")
        If Not String.IsNullOrEmpty(answer) Then
            CType(uc1, IAnswerUC).Answer = answer
        End If
        Response.Write(Session("Status"))
    End Sub

    Protected Function IsCurrent(ByVal index As Integer) As String
        If CInt(qNode.CurrentNode.QuestionNumber.Split(".")(0)) = index Then
            Return "[" & index & "]"
        End If
        Return index
    End Function

    Protected Function IsEnabledQuestion(ByVal uid As Guid) As Boolean
        Return qNode.IsEnabledQuestion(uid)
        'qnode.GetQuestionNode(uid)
        'Return True
    End Function

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        If qNode.GetFirstNode() Is Nothing Then
            Response.Write("Already as the begining of Questionnaire")
        End If
    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        If qNode.GetNextQuestionNode(CType(uc1, IAnswerUC).Answer) Is Nothing Then
            Response.Write("The end of Questionnaire - But2")
        End If
    End Sub

    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
        If qNode.GetPreviousQuestionNode() Is Nothing Then
            Response.Write("Already as the begining of Questionnaire")
        End If
    End Sub

    Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
        If qNode.GetNextQuestionNode() Is Nothing Then
            Response.Write("Can't move to next question until the answer is saved.")
        End If
    End Sub

    Private Sub LoadMyControl()
        If qNode Is Nothing Then
            Response.Redirect("Default.aspx")
        End If
        If qNode.CurrentNode Is Nothing Then
            Exit Sub
        End If
        If uc1 Is Nothing Then
            Dim curQuestion As Question = QuestionnaireTakerODS.Provider.GetQuestion(qNode.CurrentNode.QuestionID)
            lblQuestion.Text = curQuestion.Question
            lblQuestionNumber.Text = "Q# " & qNode.CurrentNode.QuestionNumber
            If Not String.IsNullOrEmpty(curQuestion.Configuration) Then
                Dim type As String = curQuestion.Configuration.Substring(0, 1)
                Dim myXML As XmlDocument = ControlXDS.GetXmlDocument
                Dim myNode As XmlNode = myXML.SelectSingleNode("/controlList/control[@id=" & type & "]")
                uc1 = LoadControl(ResolveUrl("~/Secure/Controls/ans" & myNode.Attributes("control").Value))
                uc1.ID = "ansUC1"
                uc1.EnableViewState = False
                CType(uc1, IAnswerUC).Config = "1" & "||" & curQuestion.ID & "||" & curQuestion.Configuration
                phLoader.Controls.Add(uc1)
            End If
        End If
    End Sub

    Protected Sub Button5_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button5.Click
        qNode.GetLastActiveNode()
    End Sub

    Protected Sub QuestionNav_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles QuestionNav.ItemCommand
        Response.Write(e.CommandName & ":" & e.CommandArgument)
        qNode.GetQuestionNode(New Guid(e.CommandArgument.ToString))
    End Sub
End Class
